#!/usr/bin/perl
use strict;
use CGI;
use CGI qw/:standard/;
use CGI::Pretty qw/:standard *-table *-div/;
use FindBin '$Bin';
use lib $Bin;
use DBI;
use Data::Dumper;
use CgiUtil;

my $g_buy_cond_cnt = 6;
my $g_sell_fix_cond_cnt = 3;
my $g_sell_loop_cond_cnt = 3;
my %g_sim_data;
my $g_mail_addr;

sub get_post_data {
	print 
		start_form,
       "初始资产:",textfield('money'),p;

       foreach my $idx (1 .. $g_buy_cond_cnt) {
	       print
	       "买入条件$idx:",
	       "买前",textfield(-name=>"buy_cond_$idx\_begin",-size=>10),"天,",
	       "至买前",textfield(-name=>"buy_cond_$idx\_end",-size=>10),"天,",
	       "涨幅泛围:",textfield(-name=>"buy_cond_$idx\_rate_lower",-size=>10),"~",textfield(-name=>"buy_cond_$idx\_rate_upper",-size=>10),",",
	       "走势方向",popup_menu(-name=>"buy_cond_$idx\_forward",-values=>["涨优先","跌优先"]),",",
	       "价格泛围:",textfield(-name=>"buy_cond_$idx\_price_lower",-size=>10),"~",textfield(-name=>"buy_cond_$idx\_price_upper",-size=>10),",",
	       "条件比重:",textfield(-name=>"buy_cond_$idx\_weight",-size=>10),p;
       }

       print
	       "至少持仓天数:",textfield(-name=>"hold_day_lower",-size=>10),"天",p,
	       "至多持仓天数:",textfield(-name=>"hold_day_upper",-size=>10),"天",p;

       foreach my $idx (1 .. $g_sell_fix_cond_cnt) {
	       print
	       "卖出固定条件$idx:",
	       "第",textfield(-name=>"sell_fix_cond_$idx\_begin",-size=>10),"天",
	       "相对于第",textfield(-name=>"sell_fix_cond_$idx\_end",-size=>10),"天",
	       "涨幅低于",textfield(-name=>"sell_fix_cond_$idx\_rate_lower",-size=>10),"或高于",textfield(-name=>"sell_fix_cond_$idx\_rate_upper",-size=>10),
	       "则卖出",p,
       }
       foreach my $idx (1 .. $g_sell_loop_cond_cnt) {
	       print
	       "卖出循环条件$idx:",
	       "从第",textfield(-name=>"sell_loop_cond_$idx\_begin",-size=>10),"天",
	       "起,每",textfield(-name=>"sell_loop_cond_$idx\_end",-size=>10),"天",
	       "涨幅低于",textfield(-name=>"sell_loop_cond_$idx\_rate_lower",-size=>10),"或高于",textfield(-name=>"sell_loop_cond_$idx\_rate_upper",-size=>10),
	       "则卖出",p,
       }
       print
	       "初始资产分配比例:",textfield(-name=>"money_weight",-size=>10),p;
       print
	       "开始购买时间:",textfield(-name=>"sim_start_date",-size=>10),p,
	       "结束购买时间:",textfield(-name=>"sim_end_date",-size=>10),p,
	       "邮箱地址:",textfield(-name=>"mail_addr",-size=>10),p;

       print 
       submit,
       end_form,
       hr,"\n";

       if (param) {
	       %g_sim_data = ();
	       $g_sim_data{'money'} = param('money');
	       $g_sim_data{'hold_day_lower'} = param('hold_day_lower');
	       $g_sim_data{'hold_day_upper'} = param('hold_day_upper');
	       $g_sim_data{'money_weight'} = param('money_weight');
	       $g_sim_data{'sim_start_date'} = param('sim_start_date');
	       $g_sim_data{'sim_end_date'} = param('sim_end_date');
	       $g_mail_addr = param('mail_addr');

	       foreach my $idx (1 .. $g_buy_cond_cnt) {
		       if (
				       length(param("buy_cond_$idx\_begin")) > 0 and
				       length(param("buy_cond_$idx\_end")) > 0 and 
				       length(param("buy_cond_$idx\_rate_lower")) > 0 and
				       length(param("buy_cond_$idx\_rate_upper")) > 0 and
				       length(param("buy_cond_$idx\_forward")) > 0 and
				       length(param("buy_cond_$idx\_price_lower")) > 0 and
				       length(param("buy_cond_$idx\_price_upper")) > 0 and
				       length(param("buy_cond_$idx\_weight")) > 0 
			  ) {
			       $g_sim_data{"buy_cond_$idx\_begin"} = param("buy_cond_$idx\_begin");
			       $g_sim_data{"buy_cond_$idx\_end"} = param("buy_cond_$idx\_end");
			       $g_sim_data{"buy_cond_$idx\_rate_lower"} = param("buy_cond_$idx\_rate_lower");
			       $g_sim_data{"buy_cond_$idx\_rate_upper"} = param("buy_cond_$idx\_rate_upper");
			       $g_sim_data{"buy_cond_$idx\_forward"} = param("buy_cond_$idx\_forward");
			       $g_sim_data{"buy_cond_$idx\_price_lower"} = param("buy_cond_$idx\_price_lower");
			       $g_sim_data{"buy_cond_$idx\_price_upper"} = param("buy_cond_$idx\_price_upper");
			       $g_sim_data{"buy_cond_$idx\_weight"} = param("buy_cond_$idx\_weight");
		       }
	       }
	       foreach my $idx (1 .. $g_sell_fix_cond_cnt) {
		       if (
				       length(param("sell_fix_cond_$idx\_begin")) > 0 and 
				       length(param("sell_fix_cond_$idx\_end")) > 0 and 
				       length(param("sell_fix_cond_$idx\_rate_lower")) > 0 and 
				       length(param("sell_fix_cond_$idx\_rate_upper")) > 0
			  ) {
			       $g_sim_data{"sell_fix_cond_$idx\_begin"} = param("sell_fix_cond_$idx\_begin");
			       $g_sim_data{"sell_fix_cond_$idx\_end"} = param("sell_fix_cond_$idx\_end");
			       $g_sim_data{"sell_fix_cond_$idx\_rate_lower"} = param("sell_fix_cond_$idx\_rate_lower");
			       $g_sim_data{"sell_fix_cond_$idx\_rate_upper"} = param("sell_fix_cond_$idx\_rate_upper");
		       }
	       }
	       foreach my $idx (1 .. $g_sell_loop_cond_cnt) {
		       if (
				       length(param("sell_loop_cond_$idx\_begin")) > 0 and 
				       length(param("sell_loop_cond_$idx\_end")) > 0 and 
				       length(param("sell_loop_cond_$idx\_rate_lower")) > 0 and 
				       length(param("sell_loop_cond_$idx\_rate_upper")) > 0
			  ) {
			       $g_sim_data{"sell_loop_cond_$idx\_begin"} = param("sell_loop_cond_$idx\_begin");
			       $g_sim_data{"sell_loop_cond_$idx\_end"} = param("sell_loop_cond_$idx\_end");
			       $g_sim_data{"sell_loop_cond_$idx\_rate_lower"} = param("sell_loop_cond_$idx\_rate_lower");
			       $g_sim_data{"sell_loop_cond_$idx\_rate_upper"} = param("sell_loop_cond_$idx\_rate_upper");
		       }
	       }
	       $g_sim_data{"sim_start_date"} = param("sim_start_date");
	       $g_sim_data{"sim_end_date"} = param("sim_end_date");
       }

}

sub check_param {
	my ($ret, $msg) = (0, "输入有误!");
	my $conds;
	if (defined($g_sim_data{"buy_cond_1_begin"})) {
		foreach my $key (keys %g_sim_data) {
			if (
					$key =~ /date/ or
					$key =~ /forward/ or
					$key =~ /weight/
			   ) {
                              #not check
			} else {
				my $digit = $g_sim_data{$key};
				foreach my $dig_idx (0 .. length($digit)-1) {
					my $dig = substr($digit, $dig_idx, 1);
					if ( not (
							$dig eq "."  or
							$dig eq "-"  or
							($dig ge "0"  and $dig le "9")
						 )) {
						print "$dig,here";
						$ret = 1;
						$msg .= "$digit不是数字,";
					}
				}
			}
		}
	}
	if ($ret == 0) {
		$conds = "t_stock_price_A|"
			. $g_sim_data{"money"}."|"
			. 100 . "|";
		my $all_buy_cnt = 0;
		my $buy_str = "";
		foreach my $idx (1 .. $g_buy_cond_cnt) {
			if (defined($g_sim_data{"buy_cond_$idx\_begin"})) {
				$all_buy_cnt = $idx;
				my $m_forward = -1;
				if ($g_sim_data{"buy_cond_$idx\_forward"} eq "涨优先") {
					$m_forward = 1;
				}
				$buy_str .= $g_sim_data{"buy_cond_$idx\_begin"} . "-" . $g_sim_data{"buy_cond_$idx\_end"} . ":" . $g_sim_data{"buy_cond_$idx\_rate_lower"} . "," . $g_sim_data{"buy_cond_$idx\_rate_upper"}
				. ":" .$m_forward . "," .$g_sim_data{"buy_cond_$idx\_price_lower"} . "-" . $g_sim_data{"buy_cond_$idx\_price_upper"} . ":" . $g_sim_data{"buy_cond_$idx\_weight"} . ";";
			}
		}
		$conds .= $all_buy_cnt . "|" . $buy_str;
		$conds .= "|" . $g_sim_data{"hold_day_lower"} . "|" . $g_sim_data{"hold_day_upper"};
		$conds .= "|0.00|0.00";

		my $sell_fix_str = "";
		foreach my $idx (1 .. $g_sell_fix_cond_cnt) {
			if (defined($g_sim_data{"sell_fix_cond_$idx\_begin"})) {
				$sell_fix_str .= $g_sim_data{"sell_fix_cond_$idx\_begin"} . "-" . $g_sim_data{"sell_fix_cond_$idx\_end"} . ":"
					. $g_sim_data{"sell_fix_cond_$idx\_rate_lower"} . "," . $g_sim_data{"sell_fix_cond_$idx\_rate_upper"} . ";" ;
			}
		}
		if (length($sell_fix_str)>1) {
			$conds .= "|" . $sell_fix_str;
		}

		my $sell_loop_str = "";
		foreach my $idx (1 .. $g_sell_loop_cond_cnt) {
			if (defined($g_sim_data{"sell_loop_cond_$idx\_begin"})) {
				$sell_loop_str .= "x" . $g_sim_data{"sell_loop_cond_$idx\_begin"} . "-" . $g_sim_data{"sell_loop_cond_$idx\_end"} . ":"
					. $g_sim_data{"sell_loop_cond_$idx\_rate_lower"} . "," . $g_sim_data{"sell_loop_cond_$idx\_rate_upper"} . ";" ;
			}
		}
		if (length($sell_loop_str)>1) {
			$conds .=  $sell_loop_str;
		}
		$conds .= "|" . $g_sim_data{'money_weight'} . "|" .$g_sim_data{'sim_start_date'} . "|" . $g_sim_data{'sim_end_date'} . "|";

		my $content = "";
		my $db_name = "d_stock";
		my $db_host = "localhost";
		my $db_user = "root";
		my $db_pwd = "123456";
		my $dbh = DBI->connect("dbi:mysql:$db_name:$db_host", $db_user, $db_pwd);

		my $sql = "select c_task_id from t_daily_task where c_condition=\'$conds\'";
		my $rows = $dbh->selectall_arrayref($sql);
		my $cnt = 0;
		my $task_id = 0;
		foreach (@$rows) {
			($task_id) = @$_;
		}
		if ($task_id >0) {
			$ret = 1;
			$msg = "请忽重复提交相同任务，先前已提交的任务是：$task_id";
		}
		$msg = $conds;
	}
	return ($ret, $msg);
}

sub insert_db {
	my ($conds) = @_; 
	my $db_name = "d_stock";
	my $db_host = "localhost";
	my $db_user = "root";
	my $db_pwd = "123456";
	my $dbh = DBI->connect("dbi:mysql:$db_name:$db_host", $db_user, $db_pwd);
	my $sql = "insert into t_daily_task (c_task_status,c_condition) values (0, \'$conds\')";
	$dbh ->do($sql);
	$sql = "select c_task_id from t_daily_task where c_condition=\'$conds\'";
	my $rows = $dbh->selectall_arrayref($sql);
	my $cnt = 0;
	my $task_id = 0;
	foreach (@$rows) {
		($task_id) = @$_;
	}
	if ($task_id >0) {
		print "<br>提交成功,任务id是：$task_id,请牢记";
	}
}

sub main {
	print myhead("仿真任务提交");
	print h3("仿真任务提交");
	get_post_data();
	my ($ret, $msg) = check_param();
	print "$msg";
	if ($ret == 0) {
		insert_db($msg);
		print "<br>insert ok";
	}

    print end_html;
}

main();

